/* ****************************************************************
   ****************************************************************
   * Com++ Professional (c) 2009 C++ World
   * http://www.cplusplusworld.com/ mailto:compp@cplusplusworld.com
   ****************************************************************
   ****************************************************************
*/


#ifndef  SOCKETUNIX_H
#define  SOCKETUNIX_H

#include <iostream>
#include <cstring>
#include <string>
#include <unistd.h>
#include <fcntl.h>

#include "ComPlusPlus/Socket.h"


namespace compp {

  enum UDSTYPE { STREAM, DATAGRAM };

  /**
     Unix Socket Class. Handling connection oriented processing.
  */

  class SocketUnix : public Socket {

  private:
    void NewUnixSocket(enum UDSTYPE ST );

  protected:

    std::string       ownAddress;
    std::string       otherAddress;
    enum UDSTYPE STYP;

    void _MkSockaddrUnix( const std::string &fn,  struct sockaddr_un *ad );

  public:

    ~SocketUnix();
    SocketUnix(enum UDSTYPE ST = STREAM, unsigned int buffsz = SocketUnixStandardBufferSize);
    SocketUnix(const std::string & fn, enum UDSTYPE ST = STREAM, unsigned int buffsz = SocketUnixStandardBufferSize);
    SocketUnix(int lfd, sockaddr_un *u, size_t l, unsigned int readaheadbufsize = SocketUnixStandardBufferSize) ;
    void Shutdown (int method = 0);

    void Close ();
    
    // Listen with backlog or stdbacklog
    void Listen ( const int backlog = 5);

    // Do an explicit bind
    void Bind ( const std::string & fn);

    bool Connect( const std::string &fn );
    SocketUnix* Accept ( void );

    // Send to a UDS ( unconnected )
    int Sendto(const void *buf, size_t len, const std::string &fn);     

    // Receive from a UDS ( unconnected )
    int Recvfrom(void *buf, int len, int flags, std::string &path);

    static const unsigned int SocketUnixStandardBufferSize = (8*1024);

  }; 
}
#endif


